﻿using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.KernelExtensions;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Planning.Planners;
using SemanticKernelDemo0510.NativeSkill;
using SemanticKernelDemo0510.Utils;

var kernel = new KernelBuilder().Configure(p => {
    p.AddAzureTextCompletionService(
        ProjectEnvironment.Get("AzureOpenAIServiceId"),
        ProjectEnvironment.Get("AzureOpenAIDeploymentName"),
        ProjectEnvironment.Get("AzureOpenAIEndpoint"),
        ProjectEnvironment.Get("AzureOpenAIKey"));

    p.AddAzureTextEmbeddingGenerationService(
        ProjectEnvironment.Get("AzureOpenAIEmbeddingsServiceId"),
        ProjectEnvironment.Get("AzureOpenAIEmbeddingsDeploymentName"),
        ProjectEnvironment.Get("AzureOpenAIEmbeddingsEndpoint"),
        ProjectEnvironment.Get("AzureOpenAIEmbeddingsKey"));
}).WithMemoryStorage(new VolatileMemoryStore()).Build();

// var randomWrite =
//     kernel.ImportSemanticSkillFromDirectory(ProjectFile.GetSkillsFolder(),
//         "WriterSkill")["RandomWrite"];
// var randomWriteResult = await kernel.RunAsync(randomWrite);
// Console.WriteLine(randomWriteResult.Result);
//
// var topicWrite =
//     kernel.ImportSemanticSkillFromDirectory(ProjectFile.GetSkillsFolder(),
//         "WriterSkill")["TopicWrite"];
// var topicContextVariables = new ContextVariables("鸡你太美");
// var topicWriteResult = await kernel.RunAsync(topicContextVariables, topicWrite);
// Console.WriteLine(topicWriteResult.Result);

// kernel.ImportSkill(new DateTimeSkill(), "DateTimeSkill");
// var solarTermWrite =
//     kernel.ImportSemanticSkillFromDirectory(ProjectFile.GetSkillsFolder(),
//         "WriterSkill")["SolarTermWrite"];
// var solarTermWriteResult = await kernel.RunAsync(solarTermWrite);
// Console.WriteLine(solarTermWriteResult.Result);

// kernel.ImportSemanticSkillFromDirectory(ProjectFile.GetSkillsFolder(),
//     "WriterSkill");
// kernel.ImportSkill(new DateTimeSkill(), "DateTimeSkill");
// var planner = new SequentialPlanner(kernel);
// var plan = await planner.CreatePlanAsync("以今天的日期为主题编写一个故事");
//
// Console.WriteLine("计划:");
// Console.WriteLine(PlanToString(plan));
//
// var planResult = await kernel.RunAsync(plan);
//
// Console.WriteLine("执行结果:");
// Console.WriteLine(planResult.Result);
//
// string PlanToString(Plan originalPlan) {
//     return $"目标: {originalPlan.Description}\n\n:\n" + string.Join("\n",
//         originalPlan.Steps.Select(s =>
//             $"- {s.SkillName}.{s.Name} {string.Join(" ", s.NamedParameters.Select(p => $"{p.Key}='{p.Value}'"))}{" => " + string.Join(" ", s.NamedOutputs.Where(s => s.Key.ToUpper(System.Globalization.CultureInfo.CurrentCulture) != "INPUT").Select(p => $"{p.Key}"))}"));
// }